package com.info3k.client.commands;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.info3k.client.Info3K;
import com.info3k.client.components.InfoWindow;

public class MainCommand implements Command {
	
	private String page;
	
	public MainCommand(String page){
		this.page = page;
	}
	
	public void execute() {
		Info3K.loader.setVisible(true);
		RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, page);
		requestBuilder.setCallback(new RequestCallback(){
			public void onError(Request request, Throwable exception) {
				Info3K.loader.setVisible(false);
				InfoWindow.showMessage(Info3K.msgs.error(), exception.getMessage());
			}

			public void onResponseReceived(Request request, Response response) {
				Info3K.loader.setVisible(false);
				RootPanel.get("right_section").clear();
				RootPanel.get("right_section").add(new HTML(response.getText()));
			}
		});
		try {
			requestBuilder.send();
		} catch (RequestException e) {
			Info3K.loader.setVisible(false);
			InfoWindow.showMessage(Info3K.msgs.error(), e.getMessage());
		}
	}
}
